<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 会员管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
                <el-input v-model="select_name"  placeholder="昵称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div>
         <el-table :data="data.slice((cur_page-1)*pagesize,cur_page*pagesize)" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="id">
                </el-table-column>
                <el-table-column prop="nick_name" label="昵称">
                </el-table-column>
                <el-table-column prop="mobile" label="联系方式">
                    <template slot-scope="scope">
                        {{scope.row.mobile}}
                    </template>
                </el-table-column>
                 <el-table-column prop="balance" label="余额">
                 </el-table-column>
                 <el-table-column prop="headimgurl" label="头像">
                     <template scope="scope">
                         <img :src="scope.row.headimgurl" width="100" height="100" class="head_pic"/>
                     </template>
                 </el-table-column>
                 <el-table-column prop="member" label="是否会员">
                     <template slot-scope="scope">
                         <el-tag  disable-transitions>
                             {{scope.row.member===1?'会员':'不是'}}
                         </el-tag>
                     </template>
                 </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" :page-size="pagesize">
                </el-pagination>
            </div>
        </div>


        <!-- 编辑弹出框 -->
        <el-dialog title="修改" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="100px">
                <el-form-item label="余额">
                    <el-input v-model="form.balance"></el-input>
                </el-form-item>
                <el-form-item label="联系方式">
                    <el-input v-model="form.mobile"></el-input>
                </el-form-item>
                <el-form-item label="是否会员">
                    <el-select value="" v-model="form.member">
                        <el-option value="不是">不是</el-option>
                        <el-option value="会员">会员</el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit()">确 定</el-button>
            </span>
        </el-dialog>


        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        name: 'User',
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [],
                cur_page: 1,
                total:0,
                pagesize:20,
                multipleSelection: [],
                select_name: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                id:0,
                form: {
                    balance: '',
                    mobile:'',
                    member:''
                },
                delform: {
                   deleted_at:''
                },
                idx: -1
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
             return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.id === this.del_list[i].id) {
                            is_del = true;
                            break;
                        }
                    }
                    return d;
             })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },

            getData() {
                this.$axios({
                    method: 'get',
                    url: 'http://local.guoxiaojian.com/admin/Users?search='+this.select_name+'&page='+this.cur_page+'&pageSize='+this.pagesize,
                }).then((data) => {//这里使用了ES6的语法
                    this.total = data.data.total;
                    if(this.is_search === true){
                        this.tableData = data.data.data;
                        this.cur_page =1;
                        this.pagesize=20;
                        this.total=this.tableData.length;
                        //console.log(this.total);
                        this.is_search = false;
                    }else {
                        this.tableData = this.tableData.concat(data.data.data);
                    }
                });

            },
            search() {
                if (this.select_name === "") {
                    this.$message.warning("查询条件不能为空！");
                    return;
                }
                this.is_search = true;
                this.getData();
            },
            handleEdit(index, row) {
                this.idx = index;
                this.editVisible = true;
                this.form = Object.assign({}, row);
                if(this.form.member===1){
                    this.form.member='会员';
                }else{
                    this.form.member='不是';
                }
            },
            handleDelete(index, row) {
                this.idx = index;
                this.id=this.idx+1;
                console.log(this.id);
                this.delVisible = true;
                this.delform.deleted_at = '';
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            // 保存编辑
            saveEdit() {
                let List = this.form;
                let __this = this;
                console.log(List);
                let {balance} = List;
                let {mobile} = List;
                let {member} = List;
                if (balance === '' && mobile === '' && member ==='') {
                  return false;
                }else if(!(/^1[3|4|5|8][0-9]\d{4,8}$/).test(mobile)){
                    __this.$message.error("联系方式格式不正确!");
                    return false;
                }else {
                        this.$axios.post('http://local.guoxiaojian.com/admin/Users/Update/Users', this.form).then(res => {
                            let {errCode} = res.data;
                            if (!errCode === 1) {
                                __this.init();
                            } else {
                            }
                            __this.$set(__this.tableData, __this.idx, __this.form);
                            __this.$message.success(`修改成功`);
                            __this.editVisible = false;
                        }, response => {
                            console.log(111);
                        });
                    }
        },
            // 确定删除
            deleteRow() {
                let List = this.delform;
                let __this = this;
                console.log(List);
                let {deleted_at} = List;
                    if (deleted_at !== '') {
                        __this.$message.error('删除失败');
                    }else{
                        this.$axios.get('http://local.guoxiaojian.com/admin/Users/Delete/'+this.id).then(res => {
                            let {errCode} = res.data;
                            if (!errCode === 1) {
                                __this.init();
                            } else {

                            }
                            __this.tableData.splice(__this.idx, 1);
                            __this.$message.success(`删除成功`);
                            __this.delVisible = false;
                        }, response => {
                            console.log(111);
                        });
                }
                /*this.tableData.splice(this.idx, 1);
                this.$message.success('删除成功');
                this.delVisible = false;*/
            }

        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
</style>


